﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace MapEditor.Web
{
    public class ObjectImage
    {
        public string ImageUrl;
        public int Direction;
        public int AnimationType;
        public int FramesPerRow;
        public int FrameCount;
        public string PreviewUrl;
    }

    public class BaseObject   
    {
        public int BaseObjectID;
        public string Name;
        public bool Walkable;
        public bool Targetable;
        public bool Attackable;
        public ObjectType ObjType;
        public List<ObjectImage> ObjImages;
        public int Width;
        public int Height;
    }

    public class OpacityResource
    {
        public int OpacityMaskID;
        public string OpacityUrl;
        public string Name;
    }

    public enum ObjectType
    {
        Terrain=0,
        Map=1,
        Game=2,
        Creature=3,
        Effect=4,
    }

    public enum AnimationType
    {
        Unknown = -1,
        Walk = 0,
        Fly = 1,
        Attack = 2,
        Hit = 3,
        Death = 4,
        Idle=5,
        Run=6,
        End=7,
    }

    public enum Direction
    {
        Unknown = -1,
        North = 0,
        NE = 1,
        East = 2,
        SE = 3,
        South = 4,
        SW = 5,
        West = 6,
        NW = 7,
        End = 5,
    }

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MapEditorService
    {
        public static string DatabaseName = "SLCONTEST";
        //string _connectionStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=mtl-lap-aa125\sqlexpress.D:\PROJECTS\MAPEDITOR\MAPEDITOR.WEB\APP_DATA\MAPEDITOR.MDF.dbo;Integrated Security=True;";//User Instance=True;";
        string _connectionStr = @"Data Source=.; AttachDbFilename=|DataDirectory|\MapEditor.mdf;Integrated Security=True;Initial Catalog=MAPEDITOR";

        private string GetTableByType(ObjectType type)
        {
            switch (type)
            {
                case ObjectType.Creature:
                    return "CreatureObjects";
                case ObjectType.Effect:
                    return "EffectObjects";
                case ObjectType.Game:
                    return "GameObjects";
                case ObjectType.Map:
                    return "MapObjects";
                case ObjectType.Terrain:
                    return "TerrainObjects";
            }

            return "Unknown";
        }

        private List<ObjectImage> GetObjectImages(int baseObjectID)
        {
            List<ObjectImage> images = new List<ObjectImage>();

            SqlConnection sqlConnection = new SqlConnection(_connectionStr);
            sqlConnection.Open();

            SqlCommand sCmd = new SqlCommand("SELECT ImageUrl, Direction, AnimationType, FramesPerRow, FrameCount, PreviewUrl FROM dbo.Images WHERE BaseObjectID=" + baseObjectID, sqlConnection);

            SqlDataReader reader = sCmd.ExecuteReader();
            
            while (true == reader.Read())
            {
                ObjectImage img = new ObjectImage();
                img.AnimationType = (int)reader["AnimationType"];
                img.Direction = (int)reader["Direction"];
                img.ImageUrl = (string)reader["ImageUrl"];
                img.FramesPerRow = (int)reader["FramesPerRow"];
                img.FrameCount = (int)reader["FrameCount"];
                img.PreviewUrl = reader.IsDBNull(reader.GetOrdinal("PreviewUrl")) ? null : (string)reader["PreviewUrl"];
                images.Add(img);
            }

            reader.Close();

            sqlConnection.Close();

            return images;
        }

        [OperationContract]
        public List<BaseObject> GetBaseObjects()
        {
            List<BaseObject> baseObjects = new List<BaseObject>();            

            SqlConnection sqlConnection = new SqlConnection(_connectionStr);
            sqlConnection.Open();
            
            SqlCommand sCmd = new SqlCommand("SELECT BaseObjectID, Name, Walkable, Targetable, Attackable, ObjectType, Width, Height FROM dbo.BaseObjects", sqlConnection);
            SqlDataReader reader = sCmd.ExecuteReader();
            while (true == reader.Read())
            {
                BaseObject baseObject = new BaseObject();

                baseObject.BaseObjectID = (int)reader["BaseObjectID"];
                baseObject.Name = (string)reader["Name"];
                baseObject.Walkable = (bool)reader["Walkable"];
                baseObject.Targetable = (bool)reader["Targetable"];
                baseObject.Attackable = (bool)reader["Attackable"];
                baseObject.ObjType  = (ObjectType)reader["ObjectType"];
                baseObject.ObjImages = GetObjectImages(baseObject.BaseObjectID);
                baseObject.Width = (int)reader["Width"];
                baseObject.Height = (int)reader["Height"];

                baseObjects.Add(baseObject);

            }
            reader.Close();
            sqlConnection.Close();

            return baseObjects;
        }
        [OperationContract]
        public List<OpacityResource> GetOpacityObjects()
        { 
             List<OpacityResource> opacityObjects = new List<OpacityResource>();
             
            SqlConnection sqlConnection = new SqlConnection(_connectionStr);
            sqlConnection.Open();
            
            SqlCommand sCmd = new SqlCommand("SELECT OpacityMaskID, Name, OpacityURL FROM dbo.OpacityMasks", sqlConnection);

            SqlDataReader reader = sCmd.ExecuteReader();
            while (true == reader.Read())
            {
                OpacityResource opacityObject = new OpacityResource();

                opacityObject.OpacityMaskID = (int)reader["OpacityMaskID"];
                opacityObject.Name = (string)reader["Name"];
                opacityObject.OpacityUrl = (string)reader["OpacityURL"];

                opacityObjects.Add(opacityObject);

            }
            reader.Close();
            sqlConnection.Close();

            return opacityObjects;
        }

        [OperationContract]
        public int GetObjectCount(ObjectType objectType)
        {
            int count = 0;
            string dboTable = GetTableByType(objectType);

            SqlConnection sqlConnection = new SqlConnection(_connectionStr);
            sqlConnection.Open();

            string cmdString = "SELECT Total=count(*) FROM " + dboTable;

            SqlCommand sCmd = new SqlCommand(cmdString, sqlConnection);

            SqlDataReader reader = sCmd.ExecuteReader();
            if (true == reader.Read())
            {
                count = (int) reader["Total"];
            }
            reader.Close();
            sqlConnection.Close();

            return count;
        }
        
    }
}
